	OUTPUT "rom48k.rom"


RSLREG	equ	0x0138
EXPTBL	equ	0xFCC1
ENASLT	equ	0x0024

FORCLR 	equ 0xF3E9  ; Foreground color (+1 = background color , +1 border)
CLIKSW 	equ 0xF3DB  ; bios click switch

SLTTBL 	equ 0FCC5h



	org     0
                        
String0:
	db	32,"Hello, I am in the first RAM page"
String1:
	db	35,"second string test on first RAM page"
	
	ds      04000h-$,0FFh       ; Rellenamos hasta 16k



	org		04000h
	
	db		041h,042h
	dw		initmain
	dw		0,0,0,0,0,0

initmain:
	di
	im		1
	ld		sp,0F380h
		
	call    search_slotset		
	
	ei	
	call 	screen0
	
		
	halt	
	
	
	ld		hl,String2
	ld		de,buffer
	ld		bc,31
	ldir
	
	ld		de,0
	call	print_buffer

	halt
	
	call	0x009f			;wait keystroke	
	
	


	ld		hl,String3
	ld		de,buffer
	ld		bc,31
	ldir

	ld		de,40
	call	print_buffer

	halt

	call	0x009f			;wait keystroke	
	
		
	
	di
	call	setrompage0

	ld		hl,String0
	ld		de,buffer
	ld		bc,33
	ldir
		
	call	recbios
	ei
		
	ld		de,80
	call	print_buffer
		
	halt

	call	0x009f			;wait keystroke	
		



	di
	call	setrompage0

	ld		hl,String1
	ld		de,buffer
	ld		bc,36
	ldir
		
	call	recbios
	ei
		
	ld		de,120
	call	print_buffer
		
	halt

	;call	0x009f			;wait keystroke	

		
		
		
	jr	$
		
	ret
	
String2:
	db	30,"This string is on the 2nd page"
	

screen0:
	xor		a
	call	0x005F			;screen 0

	call	0x00C3			;cls

	ret


	
;de dest
print_buffer:
	ld		hl,buffer
	ld		a,[hl]
	inc		hl
	ld		b,0
	ld		c,a
	call	0x005c

	ret




	
; *** RUTINAS DE SLOT *** 


; -----------------------
; SEARCH_SLOTSET
; Posiciona en pagina 2
; Nuestro ROM.
; -----------------------

search_slotset:
				        call	search_slot
				        ld		(slotvar),a
				
        				jp		ENASLT


; -----------------------
; SEARCH_SLOT
; Busca slot de nuestro rom
; Siempre que se ejecute
; En 04000h-07FFFh (pagina1)
; -----------------------

search_slot:
				
				        call	0138h
				        rrca
				        rrca
				        and		3
				        ld		c,a
				        ld		b,0
				        ld		hl,0FCC1h
				        add		hl,bc
				        ld		a,(hl)
				        and		080h
				        jr		z,search_slot0
				        or		c
				        ld		c,a
				        inc		hl
				        inc		hl
				        inc		hl
				        inc		hl
				        ld		a,(hl)
				        and		0Ch
search_slot0:
				        or		c
				        ld		h,080h
				        ret
		
		
		
; ---------------------
; SEARCH_SLOTRAM
; Busca el slot de la ram
; Y almacena
; ----------------------				
				
search_slotram:
				        call	0138h
				        rlca
				        rlca
				        and		3
				        ld		c,a
				        ld		b,0
				        ld		hl,0FCC1h
				        add		hl,bc
				        ld		a,(hl)
				        and		080h
				        jr		z,search_slotram0
				        or		c
				        ld		c,a
				        inc		hl
				        inc		hl
				        inc		hl
				        inc		hl
				        ld		a,(hl)
				        rlca
				        rlca
				        rlca
				        rlca
				        and		0Ch
search_slotram0:
				        or		c
				        ld		(slotram),a
    				
	        			ret
			
			
; ------------------------------
; SETROMPAGE0			
; Posiciona nuestro cartucho en 
; Pagina 0
; -----------------------------

setrompage0:			
				
        				ld		a,(slotvar)		
				        jr		setslotpage0	
				
			
			
; ------------------------------
; RECBIOS
; Posiciona la bios ROM
; -------------------------------
					
recbios:		
        				ld		a,(EXPTBL)
				
			

; ---------------------------
; SETSLOTPAGE0
; Posiciona el slot pasado 
; en pagina 0 del Z80
; A: Formato FxxxSSPP
; ----------------------------
            
setslotpage0:       
	di

	ld      b,a                 ; B = Slot param in FxxxSSPP format                


	in      a,(0A8h)
	and     011111100b
	ld      d,a                 ; D = Primary slot value

	ld      a,b         

	and     03  
	or      d
	ld      d,a                 ; D = Final Value for primary slot 


	out     (0A8h),a

	; Check if expanded
	ld      a,b
	bit     7,a
	ret     z   

	and     03h                             
	rrca
	rrca
	and     011000000b
	ld      c,a                 
	ld      a,d
	and     00111111b
	or      c
	ld      c,a                 ; Primary slot value with main slot in page 3  

	ld      a,b
	and     00001100b
	rrca
	rrca    
	and     03h
	ld      b,a                 ; B = Expanded slot in page 3
	ld      a,c
	out     (0A8h),a            ; Slot : Main Slot, xx, xx, Main slot
	ld      a,(0FFFFh)
	cpl
	and     011111100b
	or      b
	ld      (0FFFFh),a          ; Expanded slot selected 

	ld      c,a

								; Slot Final. Ram, rom c, rom c, Main
	ld      a,d                 ; A = Final value
	out     (0A8h),a

	and     3                   ; Set value in STLTBL 
	ld      de,SLTTBL    
	add     a,e
	ld      e,a
	jr      nc,.nocarry
	inc     d
.nocarry:
	ld      a,c
	ld      (de),a

	ret

                    
setslotpage0_end:









	
	ds		0x8000 - $,0FFh
	
	org	0x8000
	
String3:
	db	30,"This string is on the 3th page"

	ds		0xc000 - $,0FFh	
	
	org	0xc000

slotvar	equ		0xc000      +1
slotram equ		slotvar 	+1
position equ	slotram		+1
buffer	equ		position	+1
	
